package top.healthylife.docx4jhelper.utils;

import lombok.experimental.UtilityClass;

import java.util.HashMap;
import java.util.Map;

/**
 * @author maoxiaomeng
 */
@UtilityClass
public class DocumentContext {
    static ThreadLocal<Map<String, byte[]>> resourceCache = new ThreadLocal<>();

    public static void setCache(String key, byte[] value) {
        Map<String, byte[]> stringMap = resourceCache.get();
        if (stringMap == null) {
            stringMap = new HashMap<>(128);
            resourceCache.set(stringMap);
        }
        resourceCache.get().putIfAbsent(key, value);
    }

    public static boolean containsKey(String key) {
        Map<String, byte[]> stringMap = resourceCache.get();
        if (stringMap == null) {
            stringMap = new HashMap<>(128);
            resourceCache.set(stringMap);
        }
        return resourceCache.get().containsKey(key);
    }

    public static void clearCache() {
        Map<String, byte[]> stringMap = resourceCache.get();
        if (stringMap == null) {
            stringMap = new HashMap<>(128);
            resourceCache.set(stringMap);
        }
        resourceCache.remove();
    }

    public static void clearKey(String key) {
        Map<String, byte[]> stringMap = resourceCache.get();
        if (stringMap == null) {
            stringMap = new HashMap<>(128);
            resourceCache.set(stringMap);
        }
        resourceCache.get().remove(key);
    }

    public static byte[] getCache(String key) {
        Map<String, byte[]> stringMap = resourceCache.get();
        if (stringMap == null) {
            stringMap = new HashMap<>(128);
            resourceCache.set(stringMap);
        }
        return resourceCache.get().get(key);
    }

}